package ru.hangman.donkeykong.background;

import java.util.ArrayList;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import android.content.Context;

public class Background {

	private class TexturesData {
		BitmapTextureAtlas _atlas = null;
		TextureRegion _region = null;
		
		public TexturesData(Context context, int width, int height, String path) {
			_atlas = new BitmapTextureAtlas(width, height, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
			_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
	        		_atlas, context, path, 0, 0);
		}
		
		BitmapTextureAtlas getAtlas() {
			return _atlas;
		}
		
		TextureRegion getRegion() {
			return _region;
		}
	}

	private class SpriteData {
		Sprite _sprite;
		int _number;
		
		public SpriteData(int x, TextureRegion region, int number) {
	        _sprite = new Sprite(x, 0, region);
	        _sprite.setHeight(_height);
	        _sprite.setZIndex(0);
	        _number = number;
		}
		
		public Sprite getSprite() {
			return _sprite;
		}
		
		public int getNumber() {
			return _number;
		}
	}
	
	private ArrayList<TexturesData> texturesData = new ArrayList<TexturesData>();
	private BaseGameActivity _activity;
	private ArrayList<SpriteData> _sprites = new ArrayList<SpriteData>(2);
	private int _height = 0;
	private int _width = 0;
	
    public Background(BaseGameActivity activity, int height, int width) {
    	_activity = activity;
    	_height  = height;
    	_width = width;
    }	
    
    public void addTexture(int width, int height, String path) {
    	TexturesData data = new TexturesData(_activity, width, height, path);
    	texturesData.add(data);
        _activity.getEngine().getTextureManager().loadTexture(data.getAtlas());
    }
    
    public int getBackgroundLength() {
    	int width = 0;
    	for(TexturesData data: texturesData)
    		width += data.getRegion().getWidth();
    	return width;
    }
    
    public void draw(final Scene scene, int offset) {
    	int regionWidth = texturesData.get(0).getRegion().getWidth();
    	int id = offset/regionWidth;
    	int off = offset - id * regionWidth;
    	int currentWidth = 0;
    	int drawingWidth = regionWidth - off;
    	int count = 0;
    	do {
    		drawOnScene(scene, id + count, count, off);
    		currentWidth += drawingWidth;
    		drawingWidth = regionWidth;
    		count++;
    	}
    	while(currentWidth < _width);
    }
    
    public void update(int offset) {
    	draw(_activity.getEngine().getScene(), offset);
    }
    
    private void drawOnScene(final Scene scene, int id, int num, int offset) {
    	if(id >= texturesData.size())
    		return;
    	TextureRegion region = texturesData.get(id).getRegion();
    	if((num >= _sprites.size()) || (_sprites.get(num).getNumber() != id)) {
    		if(num < _sprites.size()) {
    			scene.detachChild(_sprites.get(num).getSprite());
				_sprites.set(num, new SpriteData(-offset + num*region.getWidth(), region, id));
    		}
    		else 
    			_sprites.add(new SpriteData(-offset + num*region.getWidth(), region, id));
    		
        	Sprite sprite = _sprites.get(num).getSprite();
            sprite.setHeight(_height);
            scene.attachChild(sprite);
    	}
    	else {
    		_sprites.get(num).getSprite().setPosition(-offset + num*region.getWidth(), 0);
    	}
    }
}
